package com.nicolaspigelet.dopplrclient.UI 
{
	import com.dopplr.api.Traveller;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import org.flashdevelop.utils.FlashConnect;
	
	/**
	 * @author Nicolas Pigelet aka tanaki
	 */
	public class UserUI extends Sprite
	{
		private var __coolvetica:Font;
		private var __container:Sprite;
		private var __loader:Loader;
		private var __urRequest:URLRequest;
		private var __traveller:Traveller;
		private var text:TextField;
		
		public function UserUI(traveller:Traveller) 
		{
			__traveller = traveller;
			__coolvetica = new Coolvetica();
			
			__container = new Sprite();
			__container.graphics.beginFill(0xffffff);
			__container.graphics.drawRect(0, 0, 220, 65);
			__container.filters = [new DropShadowFilter(2, 45, 0, .4, 6, 6, 1, 3)];
			
			text = new TextField();
			text.embedFonts = true;
			text.multiline = true;
			text.defaultTextFormat = new TextFormat(__coolvetica.fontName, 18, 0x000000, false, false, false, null, null, TextFormatAlign.LEFT);
			text.text = "Loading...";
			text.x = 15;
			text.y = 5;
			text.height = 20;
			text.width = 190;
			text.mouseEnabled = false;
			__container.addChild(text);
			
			__urRequest = new URLRequest(__traveller.fullIconURL);
			__loader = new Loader();
			__loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __loadComplete);
			__loader.load(__urRequest);
			
			addChild(__container);
		}
		
		private function __loadComplete(e:Event):void 
		{
			var img : Bitmap = Bitmap(__loader.content);
			img.width = 50;
			img.height = 50;
			img.x = 10;
			img.y = 7;
			addChild(img);
			
			text.text = __traveller.name;
			text.x = 65;
			
			var status : TextField = new TextField();
			status.embedFonts = true;
			status.autoSize = TextFieldAutoSize.LEFT;
			status.defaultTextFormat = new TextFormat(__coolvetica.fontName, 16, 0x000000, false, false, false, null, null, TextFormatAlign.LEFT);
			status.appendText( __traveller.status );
			status.height = 20;
			status.x = 65;
			status.y = text.y + text.height;
			status.mouseEnabled = false;
			__container.addChild(status);
			
			dispatchEvent(new Event(Event.INIT));
		}
		
	}

}